/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tomasio.projects.welcome.dao;

import com.tomasio.projects.welcome.dto.ProibicaoDeAcessoDTO;
import com.tomasio.projects.welcome.model.ProibicaoDeAcesso;
import com.tomasio.projects.welcome.util.HibernateUtil;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 *
 * @author administrador
 */
public class HibernateProibicaoDeAcessoDAO implements ProibicaoDeAcessoDAO {

    private SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
   

    @Override
    public ProibicaoDeAcessoDTO find(Long id) {

        
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        ProibicaoDeAcesso proibicao = (ProibicaoDeAcesso) session.get(ProibicaoDeAcesso.class, id);
        tx.commit();
        session.close();
        ProibicaoDeAcessoDTO proibicaoDTO = proibicao.createDTO();
        return proibicaoDTO;
    }

    @Override
    public void create(ProibicaoDeAcessoDTO proibicao) {
        ProibicaoDeAcesso _proibicao =
                new ProibicaoDeAcesso(proibicao);
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        session.save(_proibicao);
        tx.commit();
        session.close();
    }

    @Override
    public void remove(Long id) {
        ProibicaoDeAcessoDTO dto = find(id);
        ProibicaoDeAcesso proibicao = new ProibicaoDeAcesso(dto);
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        session.delete(proibicao);
        tx.commit();
        session.close();

    }

    @Override
    public void update(ProibicaoDeAcessoDTO proibicao) {
        ProibicaoDeAcesso _proibicao =
                new ProibicaoDeAcesso(proibicao);
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        session.update(_proibicao);
        tx.commit();
        session.close();
    }

    @Override
    public List<ProibicaoDeAcessoDTO> findAll() {
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        List<ProibicaoDeAcesso> proibicoes = session.createQuery("from ProibicaoDeAcesso proibicao").list();
        tx.commit();
        session.close();

        List<ProibicaoDeAcessoDTO> dtoList = new ArrayList<ProibicaoDeAcessoDTO>();
        for (ProibicaoDeAcesso proibicaoDeAcesso : proibicoes) {
            dtoList.add(proibicaoDeAcesso.createDTO());
        }
        return dtoList;
    }

    @Override
    public List<ProibicaoDeAcessoDTO> findAll(Long transeunteID) {
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        List<ProibicaoDeAcesso> proibicoes = session
                .createQuery("from ProibicaoDeAcesso proibicao where proibicao.transeunteId=" + transeunteID)
                .list();
        tx.commit();
        session.close();

        List<ProibicaoDeAcessoDTO> dtoList = new ArrayList<ProibicaoDeAcessoDTO>();
        for (ProibicaoDeAcesso autorizacao : proibicoes) {
            dtoList.add(autorizacao.createDTO());
        }
        return dtoList;
    }
}
